package com.dlxy.controller;

import com.Kapicommon.exception.BusinessException;
import com.Kapicommon.exception.ErrorCode;
import com.Kapicommon.model.entity.InterfaceInfo;
import com.Kapicommon.model.entity.UserInterfaceInfo;
import com.Kapicommon.response.BaseResponse;
import com.Kapicommon.service.UserInterfaceInfoService;
import com.Kapicommon.utils.ResponseUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dlxy.annotation.AuthCheck;
import com.Kapicommon.response.BaseResponse;
import com.dlxy.mapper.UserInterfaceInfoMapper;
import com.dlxy.model.vo.InterfaceInfoVO;
import com.dlxy.service.InterfaceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  接口数据分析
 *
 * @author kjwka
 */
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {

    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;


    @Resource
    private InterfaceInfoService interfaceInfoService;

    private static final int listToplimit=3;

    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfo() {
//        List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoMapper.listTopInvokeInterfaceInfo(3);

        //查询前三total接口
        //todo 利用mybatis-plus 实现复杂sql的编写
//        QueryWrapper<UserInterfaceInfo> queryWrapper= new QueryWrapper<UserInterfaceInfo>();
//        queryWrapper.groupBy("interfaceInfoId");
//        queryWrapper.select("*, SUM(totalNum) as totalNum");
//        queryWrapper.orderByDesc("totalNum");
//        queryWrapper.last("LIMIT 3");
        //查询前三总调用次数接口
        List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoService.listTopInvokeInterfaceInfo(listToplimit);

        //遍历赋值
        List<InterfaceInfoVO> interfaceInfoVOList = userInterfaceInfoList.stream().map(userInterfaceInfo -> {
            //1.根据id来进行查询，查询到相应接口信息，
            InterfaceInfo interfaceInfo = interfaceInfoService.getById(userInterfaceInfo.getInterfaceInfoId());
            //2.将信息存到interfacefoVO中
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            //工具类 参数(重点)
            BeanUtils.copyProperties(interfaceInfo,interfaceInfoVO);
            int totalNum =userInterfaceInfo.getTotalNum();
            interfaceInfoVO.setTotalNum(totalNum);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
        for (InterfaceInfoVO interfaceInfoVO1:interfaceInfoVOList){
            System.out.println("接口信息："+interfaceInfoVO1);
        }
        return ResponseUtil.success(interfaceInfoVOList);

    }
}
